//
//  SongsPageTableViewCell.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/12.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "SongsPageTableViewCell.h"
#import "SongsTableViewCell.h"
#import "CustomerNetworking.h"
#import "FirstMoreViewController.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)

@implementation SongsPageTableViewCell
- (void)dealloc
{
    [_songsPageModel release];
    [_tableView release];
    [_titleLabel release];
    [_subLabel release];
    [_moreButton release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self lloadData];
        
        [self createSubViews];
    }
    return self;
}

-(void)lloadData
{
    self.songsArray = [NSMutableArray array];
    [CustomerNetworking customerWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1447252806&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89679373280231&longtitude=121.5286730881665" andParamer:nil andBlock:^(id response) {
        NSArray *songsArray = [NSArray arrayWithArray:[[[[[response objectForKey:@"data"] objectAtIndex:2] objectForKey:@"data"] objectAtIndex:0] objectForKey:@"songs"]];
        for (NSDictionary *dic in songsArray) {
            SongsModel *songModel = [SongsModel modelWithDictionary:dic];
            [self.songsArray addObject:songModel];
        }
        
        [self.tableView reloadData];
    }];
    
    
    
}

-(void)createSubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT_PRO(30), SCREEN_WIDTH, HEIGHT_PRO(210)) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableView];
    [_tableView release];
    
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.subLabel = [[UILabel alloc] init];
    self.subLabel.textColor = [UIColor grayColor];
    self.subLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:self.subLabel];
    [_subLabel release];
    
    self.moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.moreButton addTarget:self action:@selector(moreSongs:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.moreButton];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    
    CGRect titleRect = [self.titleLabel.text boundingRectWithSize:CGSizeMake(320, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.titleLabel.font,NSFontAttributeName, nil] context:nil];
    self.titleLabel.frame = CGRectMake(WIDTH_PRO(10), HEIGHT_PRO(10), titleRect.size.width, HEIGHT_PRO(20));
    self.subLabel.frame = CGRectMake(self.titleLabel.frame.origin.x + self.titleLabel.frame.size.width + WIDTH_PRO(10), HEIGHT_PRO(10), WIDTH_PRO(100), HEIGHT_PRO(20));
    self.moreButton.frame = CGRectMake(SCREEN_WIDTH - WIDTH_PRO(60), HEIGHT_PRO(10), WIDTH_PRO(50), HEIGHT_PRO(20));
}

-(void)setSongsPageModel:(SongsPageModel *)songsPageModel
{
    if (_songsPageModel != songsPageModel) {
        [_songsPageModel release];
        _songsPageModel = [songsPageModel retain];
    }
    
    self.titleLabel.text = songsPageModel.name;
    self.subLabel.text = songsPageModel.desc;
    [self.moreButton setTitle:@"更多 >" forState:UIControlStateNormal];
    self.moreButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.moreButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
}

+(SongsPageTableViewCell *)songsPageTableViewCellWith:(UITableView *)tableView
{
    static NSString *cellIdentify = @"cell";
    SongsPageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[SongsPageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _songsArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT_PRO(70);
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SongsModel *songModel = [_songsArray objectAtIndex:indexPath.row];
        SongsTableViewCell *cell = [SongsTableViewCell songsTableViewCellWith:tableView];
        cell.songsModel = songModel;
    cell.floorLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        return cell;
   
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SongsModel *songModel = [_songsArray objectAtIndex:indexPath.row];
//    if (songModel.auditionList.count != 0) {
//        if (songModel.auditionList.count == 3){
            [self.delegate pushModel:songModel];
    NSLog(@"1234%@", songModel.auditionList[0]);
//        }else if (songModel.auditionList.count == 2){
//            [self.delegate pushUrl:[songModel.auditionList[1] objectForKey:@"url"] andPic:songModel.picUrl andName:songModel.name];
//        }else {
//            [self.delegate pushUrl:[songModel.auditionList[0] objectForKey:@"url"] andPic:songModel.picUrl andName:songModel.name];
       // }

//    }else{
//        
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"该歌曲无法播放" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
//            NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
//        }];
//        [alert addAction:cancelAction];
//    //    [self presentViewController:alert animated:YES completion:nil];
//
//
//    }
    
}

-(void)moreSongs:(id)sender
{
    [self.delegate pushFirstMoreView];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
